﻿using Jay.Sc2.Bof.Lib.State;

namespace Jay.Sc2.Bof.Lib.Action.Terran
{
	public class ActionProduceViking : ActionProduceTerran
	{
		public static ActionProduceViking Instance = new ActionProduceViking();

		private ActionProduceViking() : base(42, 150, 75, 2) { }

		public override int AvailableProducer(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return
				(state.Starport.AvailableForProduction) +
				(state.StarportTechLab.AvailableForProduction) +
				(state.StarportReactor.AvailableForProduction);
		}

		public override bool HaveTechnology(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return
				(state.Starport.Total) +
				(state.StarportTechLab.Total) +
				(state.StarportReactor.Total) > 0;
		}

		public void OnProductionFinished(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			state.Viking++;
		}

		public override void Execute(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			int fromStarport = (state.Starport.AvailableForProduction);
			int fromStarportTechLab = (state.StarportTechLab.AvailableForProduction);
			int fromStarportReactor = (state.StarportReactor.AvailableForProduction);
			if (fromStarportReactor > 0)
			{
				base.Execute(bo);
				state.StarportReactor.Production++;
				;
				bo.AddAction(state.Time + BuildTime, new ActionGeneric("ActionSpawnViking", (ActionGeneric.ActionExecuteDelegate)delegate(IBuildOrder ibo)
				{
					TerranState ibostate = ibo.State as TerranState;
					ibostate.StarportReactor.Production--;
					OnProductionFinished(ibo);
				}));
			}
			else if (fromStarport > 0)
			{
				base.Execute(bo);
				state.Starport.Production++;
				bo.AddAction(state.Time + BuildTime, new ActionGeneric("ActionSpawnViking", (ActionGeneric.ActionExecuteDelegate)delegate(IBuildOrder ibo)
				{
					TerranState ibostate = ibo.State as TerranState;
					ibostate.Starport.Production--;
					OnProductionFinished(ibo);
				}));
			}
			else if (fromStarportTechLab > 0)
			{
				base.Execute(bo);
				state.StarportTechLab.Production++;
				bo.AddAction(state.Time + BuildTime, new ActionGeneric("ActionSpawnViking", (ActionGeneric.ActionExecuteDelegate)delegate(IBuildOrder ibo)
				{
					TerranState ibostate = ibo.State as TerranState;
					ibostate.StarportTechLab.Production--;
					OnProductionFinished(ibo);
				}));
			}
			else throw new InvalidActionException();
		}


		private IAction[,] m_Requirements;
		public override IAction[,] Requirements
		{
			get
			{
				if (m_Requirements == null)
					m_Requirements = new IAction[,] { { ActionProduceStarport.Instance } };
				return m_Requirements;
			}
		}

	}
}
